class MusicManager{

    private static _instance:MusicManager;//单例

    private _nowMusicId;//当前播放背景音乐的id
    private _nowMusicUrl:string;//当前播放背景音乐的url
    private _soundLoopUrl:string;//循环播放的声音url

    constructor(){
        //初始化
        Laya.SoundManager.autoStopMusic = false;
        Laya.SoundManager.useAudioMusic = false;
    }

    /**
     * 获取单例
     */
    public static getInstance():MusicManager{
        if(MusicManager._instance == null){
            MusicManager._instance = new MusicManager();
        }
        return MusicManager._instance;
    }

    /**
     * 播放背景音乐根据id
     */
    public playMusic(id:number):void{
        if(this._nowMusicId == id){
            return;
        }
        this._nowMusicId = id;
        this._nowMusicUrl =  "res/sound/" + id + ".mp3";
        this.playMusicLoop();
    }

    /**
     * 播放声音
     */
    public playSound(id:number):void{
        Laya.SoundManager.playSound("res/sound/" + id + ".wav");
    }

    /**
     * 循环播放声音
     * 不能同时播放多个
     */
    public playSoundLoop(id:number){
        if(this._soundLoopUrl != null){
            return;
        }
        this._soundLoopUrl = "res/sound/" + id + ".wav";
        Laya.SoundManager.playSound(this._soundLoopUrl,0);
    }

    /**
     * 停止循环播放声音
     */
    public stopSoundLoop():void{
        if(this._soundLoopUrl == null){
            return;
        }
        Laya.SoundManager.stopSound(this._soundLoopUrl);
        this._soundLoopUrl = null;
    }

    /**
     * 是否禁音背景音乐
     */
    public mutedMusic(value:boolean):void{
        if(Laya.SoundManager.musicMuted != value){
            Laya.SoundManager.musicMuted = value;
        }
    }

    /**
     * 是否禁音声音
     */
    public muethSound(value:boolean):void{
        if(Laya.SoundManager.soundMuted != value){
            Laya.SoundManager.soundMuted = value;
        }
    }

    //播放背景音乐
    private playMusicLoop():void{
        Laya.SoundManager.playMusic(this._nowMusicUrl,1,Handler.create(this,this.playMusicLoop));
    }
}